<?php

/** 
 * 用户设备模型
 * @property Dev_mdl $dev
 */
class User_dev_mdl extends MY_Model {
	public function __construct() {
		parent::__construct ();
		$this->load->database();
		$this->_table = parent::T_USER_DEV;
	}
	/**
	 * 获取用户设备
	 * @param unknown_type $uid
	 * @param unknown_type $devID
	 * @param unknown_type $select
	 * @return Ambigous <multitype:, NULL>
	 */
	public function getUserDev($uid,$devID,$select='*'){
		$where = array(
				'userID'=>$uid,
				'devID'=>$devID,
		);
		return $this->getOne($where,$select);
	}
	/**
	 * 获取管理员设过的设备别名
	 * @param unknown_type $uid
	 * @param unknown_type $devID
	 * @param unknown_type $select
	 * @return Ambigous <multitype:, NULL>
	 */
	public function getDevListByTest($devID,$select='*'){
		$sql = "SELECT  ".$select." FROM ".$this->_table." WHERE `userID` in ( SELECT id FROM `hs_user` WHERE isTest=1 ) and devID=".$devID." order by createTime desc ";
		 
		$list = $this->db->query($sql)->result_array();
		$data=array();
		if($list){
			$data=$list[0];
		}
		return $data;
	}
	/**
	 * 增加使用次数
	 * @param unknown_type $uid
	 * @param unknown_type $devID
	 * @return Ambigous <boolean, mixed, unknown, string>
	 */
	public function incUsedCount($uid,$devID){
		return $this->db->set('usedCount','usedCount+1',FALSE)
				->where('userID',$uid)
				->where('devID',$devID)
				->update($this->_table());
	}
	
	public function getDevListByUid($uid,$select='*'){
		$uid = (int)$uid;
		
		$sql = "SELECT u.`devID` AS id,u.`createTime`,u.`alias`,u.`usedCount`,d.`name`,d.`mac`,d.`type` 
				FROM ".$this->_table." AS u 
				LEFT JOIN ".parent::T_DEV." AS d 
					ON u.`devID`=d.`id` 
				WHERE `userID` = ?";
		$list = $this->db->query($sql,array($uid))->result_array();
		foreach ($list as $k=>$v){
			$list[$k] = $this->formatRow($v);
		}
		return array_values($list);
		
	}
	
	public function formatRow($row){
		$row = (array)$row;
		foreach ($row as $k=>$v){
			switch ($k){
				case 'id':
				case 'userID':
				case 'usedCount':
					$row[$k] = (int)$v;
					break;
				case 'name':
				case 'mac':
				case 'alias':
				case 'devUUID':
				case 'address':
					$row[$k] = (string)$v;
					break;
				case 'createTime':
					$row[$k] = (float)$v;
					break;
				default:
					$row[$k] = (string)$v;
					break;
			}
		}
		return $row;
	}
	/**
	 * 判断是否为添加过的设备
	 * @param int $userID
	 * @param int $devID
	 * @return array
	 */
	public function addedDev($userID,$devID,$alias=''){
		$where = array(
				'userID'=>$userID,
				'devID'=>$devID,
		);
		$row = $this->getOne($where);
		if ( ! empty($row) ) {
			$this->db->set('usedCount','usedCount + 1',FALSE)
						->where('userID',$userID)
						->where('devID',$devID)
						->update($this->_table);
			return $row;
		}
		if( empty($alias) ){
			$this->load->model('Dev_mdl','dev');
			$dev = $this->dev->getOneById($devID,'id,name');
			if ( empty($dev) ) {
				return FALSE;
			}
			$alias = $dev['name'];
		}
		$insert = $where;
		$insert['alias'] = $alias;
		$insert['usedCount']  = 1;
		$insert['createTime'] = time();
		
		if( ! $this->insert($insert) ){
			return FALSE;
		}
		return $insert;
	}
}

